import { Order } from 'order-model.js';
import { Evaluate } from '../evaluate/evaluate-model.js';
var order = new Order();
var evaluate = new Evaluate();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    radioItems: [
      { name: '质量问题', value: '质量问题' },
      { name: '卖家发错货', value: '卖家发错货', checked: 'true' },
      { name: '七天无理由', value: '七天无理由' },
      { name: '活动与页面图片不符', value: '活动与页面图片不符' },
      { name: '发票问题', value: '发票问题' },
      { name: '其它', value: '其它' },
    ],
    hiddenSelBox:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      status: options.status,
    })
    this.data.id = options.id;
    this._loadData(this.data.id);
  },
  _loadData:function(id){
    var args = {
      order_id : id
    }
    order.getOrderDetails(args, (data) => {
      var date = evaluate.toDate(data.item.create_time);
     
      if (this.data.status == 1) {
        this.counTtime(data.item.create_time, data.item.id)
      }
      var discount_price = Math.abs(data.item.discount_price);
      data.item.create_time = date;
      this.setData({
        orderData: data.item,
        status: data.item.status,
        discount_price: discount_price 
      })
    })
  },
  changeData:function(){
    this._loadData(this.data.id);
  },

  //倒计时
  counTtime: function (create_time,id){
      var interval = setInterval(function () {
      var endtime = (parseInt(create_time) + 2 * 3600);
      var nowtime = Date.parse(new Date()) / 1000
      var totalSecond = endtime - nowtime;
      var second = totalSecond;

      // 天数位
      var day = Math.floor(second / 3600 / 24);
      var dayStr = day.toString();
      if (dayStr.length == 1) dayStr = '0' + dayStr;

      // 小时位
      var hr = Math.floor((second - day * 3600 * 24) / 3600);
      var hrStr = hr.toString();
      if (hrStr.length == 1) hrStr = '0' + hrStr;

      // 分钟位
      var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
      var minStr = min.toString();
      if (minStr.length == 1) minStr = '0' + minStr;

      // 秒位
      var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
      var secStr = sec.toString();
      if (secStr.length == 1) secStr = '0' + secStr;
      this.setData({
        countDownDay: dayStr,
        countDownHour: hrStr,
        countDownMinute: minStr,
        countDownSecond: secStr,
      });
      totalSecond--;
      if (totalSecond < 0) {
        clearInterval(interval);
        order.scancelOrder(id, (res) => {
        })
        this.setData({
          status: 9
        })
      }
      //订单关闭


    }.bind(this), 1000);
    
  },

  // 取消订单
  _cancelOrder: function (event) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '订单还未付款，确定要取消么？',
      cancelText:'取消订单',
      confirmText:'考虑一下',
      success: function (res) {
        if (res.confirm) {
          console.log('再考虑一下')
        } else if (res.cancel) {
          var id = order.getDataSet(event, "id")
          order.cancelOrder(id, (res) => {
            console.log(11);
            if(res.status==1){
              that.setData({
                status:8
              })
              wx.showToast({
                title: '取消订单成功~',
                icon: 'none',
                duration: 2000
              })
              //获取页面栈
              var pages = getCurrentPages();
              console.log(pages);
              if (pages.length > 1) {
                //上一个页面实例对象
                var prePage = pages[pages.length - 2];
                //关键在这里,这里面是触发上个界面
                prePage.changeData()
              }
            }else{
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
            }
          })
        }
      }
    })
  },

  // 确认收货
  _confirmOrder: function (event) {
    var that = this;
    var id = order.getDataSet(event, "id")
    wx.showModal({
      title: '提示',
      content: '你是否收到该订单活动？',
      confirmText: '已收货',
      cancelText: '未收货',
      success: function (res) {
        if (res.confirm) {
          console.log('已收货')
          order.confirmOrder(id, (res) => {
            console.log(res.status)
            if (res.status == 1) {
              that.setData({
                status: 4
              })
              wx.showToast({
                title: '确认收货成功',
                icon: 'none',
                duration: 2000
              })
              //获取页面栈
              var pages = getCurrentPages();
              console.log(pages);
              if (pages.length > 1) {
                //上一个页面实例对象
                var prePage = pages[pages.length - 2];
                //关键在这里,这里面是触发上个界面
                prePage.changeData()
              }
            }
          })
        } else if (res.cancel) {
          console.log('未收货')
        }
      }
    })
  },

  // 申请退款
  toRefundOrder: function (event) {
    var id = this.data.id;
    wx.navigateTo({
      url: 'refund?id=' + id,
    })
  },

//评价晒单
  toEvaluate: function (event) {
    var id = order.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../evaluate/list?order_id=' + id
    });
  },

  /*未支付订单再次支付*/
  rePay: function (event) {
    var id = order.getDataSet(event, 'id');
    wx.navigateTo({
      url: 'payment?order_id='+id
    });
  },
  //跳转到活动详情
  toDetail: function (e) {
    var url = '../product/product?id='+e.target.dataset.id;
    wx.navigateTo({
      url: url,
    })
  }
})